package kolekcje.powtorka.trzecia;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class MusicBoxSetter {

	private String[] songs = { "Pink Moon/Nick Drake/5/80",
			"Somersault/Zero 7/4/84", "Listen/Tahiti 80/5/90",
			"Shiva Moon/Prem Joshua/6/120", "Circles/BT/5/110",
			"Deep Channel/Afro Celts/4/120", "Passenger/Headmix/4/100",
			"Listen/Tahiti 80/5/90", "Listen/Tahiti 80/5/90",
			"Listen/Tahiti 80/5/90" };
	
	static File fileSer = new File("songs.ser");
	static File fileText = new File("songs.txt");
	
	public static void main(String[] args) {

		new MusicBoxSetter().makeMusicBox();
	}

	void makeMusicBox() {
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(fileText));
			for(String e : songs)
				bw.write(e + "\n");
			System.out.println(fileText.getAbsolutePath());
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileSer));
			for(String e : songs)
				os.writeObject(e);
			System.out.println(fileSer.getAbsolutePath());
			os.close();
		} catch (FileNotFoundException e) {
			System.out.println("Pliku nie odnaleziono");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			BufferedReader br = new BufferedReader(new FileReader(fileText));
			String s;
			while((s = br.readLine()) != null)
					System.out.println(s);
			System.out.println();
			br.close();
		} catch (FileNotFoundException e) {
			System.out.println("Pliku nie odnaleziono");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			ObjectInputStream is = new ObjectInputStream(new FileInputStream(fileSer));
			for (int i = 0; i < songs.length; i++) {
				System.out.println((String) is.readObject());
			}
			System.out.println();
			is.close();
		} catch (FileNotFoundException e) {
			System.out.println("Pliku nie odnaleziono");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
